package com.shuiyun.chat.controller;

import com.google.gson.Gson;
import com.shuiyun.chat.entity.Result;
import com.shuiyun.chat.entity.bo.ChatGroupBO;
import com.shuiyun.chat.entity.bo.SendGroupMessageBO;
import com.shuiyun.chat.entity.bo.SendMsgBO;
import com.shuiyun.chat.entity.form.CreateGroupForm;
import com.shuiyun.chat.entity.message.SendGroupMessage;
import com.shuiyun.chat.service.ChatGroupService;
import com.shuiyun.chat.service.ChatService;
import com.shuiyun.chat.service.UserService;
import io.netty.channel.Channel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shuiyun
 * @program chatServer
 * @description 消息接口
 * @create 2021-05-14 16:28
 **/
@Api(tags = "聊天接口")
@RestController
@RequestMapping("chat")
public class ChatController {

    @Autowired
    private ChatService chatServer;
    @Autowired
    private ChatGroupService groupService;
    @Autowired
    private UserService userService;

    @PostMapping("sendMsg")
    @ApiOperation("消息发送")
    public Result<Object> sendMsg(SendMsgBO bo){
        chatServer.sendMsg(bo);
        return Result.ok();
    }

    @ApiOperation("创建群聊")
    @PostMapping("createGroup")
    public Result<Long> createGroup(@RequestBody CreateGroupForm form){
        form.getMembers().add(form.getUserName());
        ChatGroupBO chatGroupBO = ChatGroupBO.builder().name(form.getGroupName()).users(new Gson().toJson(form.getMembers()))
                .build();
        Long id = groupService.add(chatGroupBO);
        return Result.ok(id);
    }

    @ApiOperation("发送群聊消息")
    @PostMapping("sendGroupMessage")
    public Result<Object> sendGroupMessage(@RequestBody SendGroupMessageBO bo){
        chatServer.sendGroupMessage(bo);
        return Result.ok();
    }
}
